--
-- Author: dbliu
-- Date: 2014-02-26 22:48:00
--
--=model=========================================================================================================
local EMEnemy3 = import(".EMEnemy")
--=view=========================================================================================================
local EV = import("..EV")

local EVEnemy3 = class("EVEnemy3",EV)

function EVEnemy3:ctor(model)
	EVEnemy3.super.ctor(self,model)
	
	local cls = model.class

	--[[通过代理注册事件的好处：可以方便的在视图删除时，清理所有通过该代理注册的事件，
	同时不影响目标对象上注册的其他事件]]
	cc.EventProxy.new(model,self)
		:addEventListener(cls.START_EVENT, self.onStart, self)
		:addEventListener(cls.KILL_EVENT, self.onKill, self)

end

function EVEnemy3:removeController()
	app:remove_enemy(self)
	self:getParent():removeSelf(true)
end


function EVEnemy3:onStart( event )
	local frames = display.newFrames("enemy3_fly_%d.png", 1, 1)
	local animation = display.newAnimation(frames, 0.6 / 4)
	
	self.sprite_:playAnimationForever(animation)

	-- create body
    self.body_ = app.world:createBoxBody(100, 80,130)
    self.body_:setCollisionType(2)
    self.body_:setElasticity(0.1)
    self.body_:setIsSensor(true)
    -- binding sprite to body
    self.body_:bind(self.sprite_)
    -- set body position
    self.body_:setPosition(event.x, event.y)

    --self.body_:setVelocity(0,-80)

    
end

function EVEnemy3:onKill()
	local frames = display.newFrames("enemy3_blowup_%d.png", 1, 4)
	local animation = display.newAnimation(frames, 0.6 / 4)
	
	self.sprite_:playAnimationOnce(animation,false,handler(self, self.removeController))

	audio.playSound("music/enemy3_down.mp3")
end

function EVEnemy3:onFreeze()
	self.sprite_:setDisplayFrame(CCSpriteFrameCache:sharedSpriteFrameCache():spriteFrameByName("enemy3_hit_1.png"))
end

--=controller=========================================================================================================

local ECEnemy = import(".ECEnemy")

local ECEnemy3 = class("ECEnemy3",ECEnemy)

function ECEnemy3:ctor(x,y)

	ECEnemy3.super.ctor(self,x,y)

	self.model_ = EMEnemy3.new({id = "wx_enemy_2",blood=5})
    

    self.view_ = EVEnemy3.new(self.model_)
    :pos(display.left, display.bottom)
    :addTo(self)

    self.model_.fsm__:doEvent("start",x,y) 

    

    -- 注册帧事件
    --self:addScriptEventListener(cc.Event.ENTER_FRAME, handler(self, self.tick))

--[[
     -- 在视图清理后，检查模型上注册的事件，看看是否存在内存泄露
    self:addScriptEventListener(cc.Event.CLEANUP, function()
        self.model_:getComponent("components.behavior.EventProtocol"):dumpAllEventListeners()
    end)
]]
end

return ECEnemy3